//
//  testDev.swift
//  kano
//
//  Created by Frank on 2024/10/19.
//

import Foundation
// MARK: - Network
private func getIPAddress(isIPv4: Bool) -> String {
    var addresses = [String: String]()

    var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
    guard getifaddrs(&ifaddr) == 0 else { return "" }
    
    for ifptr in sequence(first: ifaddr!, next: { $0.pointee.ifa_next }) {
        let interface = ifptr.pointee
        let addrFamily = interface.ifa_addr.pointee.sa_family
        
        
        if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6){
            let name = String(cString: interface.ifa_name)
            if name == "en0" || name == "pdp_ip0"{
                var addr = interface.ifa_addr.pointee
                var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                getnameinfo(&addr, socklen_t(interface.ifa_addr.pointee.sa_len),
                            &hostname, socklen_t(hostname.count),
                            nil, socklen_t(0), NI_NUMERICHOST)
                let address = String(cString: hostname)
                
                if isIPv4 && addrFamily == UInt8(AF_INET) {
                    addresses["IPv4"] = address
                } else if !isIPv4 && addrFamily == UInt8(AF_INET6) {
                    addresses["IPv6"] = address
                }
            }
        }
    }

    freeifaddrs(ifaddr)
    if isIPv4 {
        return addresses["IPv4"] ?? ""
    }else{
        return addresses["IPv6"] ?? ""
    }
}

//func getNetworkType() -> String {
//       var networkType = ""
//       var zeroAddress = sockaddr()
//       memset(&zeroAddress, 0, MemoryLayout.size(ofValue: zeroAddress))
//       zeroAddress.sa_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
//       zeroAddress.sa_family = sa_family_t(AF_INET)
//       
//       guard let defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(nil, &zeroAddress) else {
//           return networkType
//       }
//       
//       var flags: SCNetworkReachabilityFlags = []
//       guard SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) else {
//           return ""
//       }
//       
//       if !flags.contains(.connectionRequired) {
//           networkType = "WIFI"
//       }
//       
//       if flags.contains(.connectionOnDemand) || flags.contains(.connectionOnTraffic){
//           if flags.contains(.interventionRequired) == false{
//               networkType = "WIFI"
//           }
//       }
//
//       if flags.contains(.isWWAN) {
//           if let info = CTTelephonyNetworkInfo().serviceCurrentRadioAccessTechnology?.values.first {
//               switch info {
//               case CTRadioAccessTechnologyGPRS:
//                   networkType = "GPRS"
//               case CTRadioAccessTechnologyEdge:
//                   networkType = "2.75G EDGE"
//               case CTRadioAccessTechnologyHSDPA:
//                   networkType = "3.5G HSDPA"
//               case CTRadioAccessTechnologyHSUPA:
//                   networkType = "3.5G HSUPA"
//               case CTRadioAccessTechnologyCDMA1x:
//                   networkType = "2G"
//               case CTRadioAccessTechnologyWCDMA,CTRadioAccessTechnologyCDMAEVDORev0,CTRadioAccessTechnologyCDMAEVDORevA,CTRadioAccessTechnologyCDMAEVDORevB:
//                   networkType = "3G"
//               case CTRadioAccessTechnologyeHRPD:
//                   networkType = "HRRB"
//               case CTRadioAccessTechnologyLTE:
//                   networkType = "4G"
//               default:
//                   networkType = "WWAN"
//               }
//               if #available(iOS 14.1, *) {
//                   if info == CTRadioAccessTechnologyNR || info == CTRadioAccessTechnologyNRNSA{
//                       networkType = "5G"
//                   }
//               }
//           }
//       }
//       if networkType.isEmpty {
//           networkType = "WWAN"
//       }
//       
//       return networkType
//   }
